import requests
from bs4 import BeautifulSoup



def parse(url):

    # 由于一般网站都是供用户访问 如果检测到User-Agent是黑客或者其他可能拒绝访问 故此处模拟浏览器
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36'
    }
    response = requests.get(url=url, headers=headers)
    # 以防乱码 此处将其编码设置为utf-8 因为有中文
    response.encoding = 'utf-8'
    # print(response.text)


    # print("开始解析网页=》=》=》")
    # print("页面地址：  "+url)

    # 通过html.parser解析器把我们的HTML解析成了一棵树
    bs = BeautifulSoup(response.text, "html.parser")
    title = bs.title.string
    # print("页面标题：  "+bs.title.string)


    picList = []
    # picNodeAList = bs.findAll("div",{"class":"pic"})
    picNodeList = bs.select(".pic img")#css选择器
    if not picNodeList:
        return {}
    for picNode in picNodeList:
        picUrl = picNode["src"]
        # print("  图片url：  "+picUrl)#获取节点属性
        picName = picUrl.rsplit('/', maxsplit=1)[1]
        pic = {"name":picName,"url":picUrl}
        picList.append(pic)
    resultJson = {"title":title,"list":picList}

    # print(resultJson)
    # print("解析结束=》=》=》")
    return resultJson;

# 测试
# url="https://www.a789xb.com/onlyfans/html/761809eda8519d3ff472865c785f60b7.html"
# resultJson = parse(url)
